package com.etc.api;

import com.etc.entity.SysUser;
import com.etc.service.SysUserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author kalista
 * @Description
 * @Date 2020/12/17  16:06
 **/
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取用户名
        String username = req.getParameter("username");
        // 2. 获取密码
        String password = req.getParameter("password");
        // 3. 数据库保存
        SysUser user = new SysUser(username,password);
        SysUserService sysUserService= new SysUserService();
        Integer add = sysUserService.add(user);
        // 4. 跳转页面
        if (add > 0){
            req.setAttribute("message","注册成功");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }else {
            req.setAttribute("message","注册失败");
            req.getRequestDispatcher("register.jsp").forward(req,resp);
        }
    }
}
